package com.ruoyi.common.vo;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.ruoyi.common.enums.RespEnum;
import com.ruoyi.common.utils.StringUtils;

import java.util.Map;

/**
 * 返回类
 * @auther: zhoudong
 * @date: 2018/12/4 13:59
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResponseModel<T> implements java.io.Serializable {
	private static final long serialVersionUID = 3346735523960666351L;
	/**
	 * @description: 成功与否
	 */
	private boolean success = false;
	/**
	 * @description: 返回业务code
	 */
	private String code;
	/**
	 * @description: 返回信息
	 */
	private String msg;
	/**
	 * @description: 返回实体
	 */
	private T data;
	/**
	 * @description: 返回扩展Map
	 */
	private Map<String,Object> extMap;
	/**
	 * @description: 返回信息
	 */
	private String cnMsg = "处理成功";

	/**
	 * 无参构造
	 */
	public ResponseModel() {

	}

	/**
	 * 有参构造
	 * @param success
	 */
	public ResponseModel(boolean success) {
		super();
		this.success = success;
		if (success) {
			msg = cnMsg;
		}
	}
	/**
	 * 有参构造
	 * @param success
	 */
	public ResponseModel(boolean success, String msg) {
		super();
		this.success = success;
		this.msg = msg;
	}
	/**
	 * 有参构造
	 * @param success
	 */
	public ResponseModel(boolean success, T data, String msg) {
		super();
		this.success = success;
		this.data = data;
		if(StringUtils.isBlank(msg)) {
			this.msg = cnMsg;
		} else {
			this.msg = msg;
		}
	}
	/**
	 * 有参构造
	 * @param success
	 */
	public ResponseModel(boolean success, T data) {
		super();
		this.success = success;
		this.data = data;
		if(success) {
			this.msg = cnMsg;
		}
	}
	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}

	public Map<String, Object> getExtMap() {
		return extMap;
	}

	public void setExtMap(Map<String, Object> extMap) {
		this.extMap = extMap;
	}

	/**
	 * 返回成功信息
	 * @auther: zhoudong
	 * @date: 2018/12/7 10:27
	 */
	public static ResponseModel doSuccess(String msg){
		return new ResponseModel(RespEnum.RESP_SUCCESS.isBooleanCode(),msg);
	}
	/**
	 * 返回成功信息
	 * @auther: zhoudong
	 * @date: 2018/12/7 10:27
	 */
	public static ResponseModel doSuccess(Object data, String msg){
		return new ResponseModel(RespEnum.RESP_SUCCESS.isBooleanCode(),data,msg);
	}
	/**
	 * 返回成功信息
	 * @auther: zhoudong
	 * @date: 2018/12/7 10:27
	 */
	public static ResponseModel doSuccess(Object data, String msg,String code){
		ResponseModel responseModel = new ResponseModel(RespEnum.RESP_SUCCESS.isBooleanCode(),data,msg);
		responseModel.setCode(code);
		return responseModel;
	}

	/**
	 * 返回失败信息
	 * @auther: zhoudong
	 * @date: 2018/12/7 10:27
	 */
	public static ResponseModel doFail(String msg){
		return new ResponseModel(RespEnum.RESP_FAIL.isBooleanCode(),msg);
	}
	/**
	 * 返回失败信息
	 * @auther: zhoudong
	 * @date: 2018/12/7 10:27
	 */
	public static ResponseModel doFail(String msg,String code){
		ResponseModel responseModel = new ResponseModel(RespEnum.RESP_FAIL.isBooleanCode(),msg);
		responseModel.setCode(code);
		return responseModel;
	}
	/**
	 * 返回失败信息
	 * @auther: zhoudong
	 * @date: 2018/12/7 10:27
	 */
	public static ResponseModel doFail(Object data, String msg,String code){
		ResponseModel responseModel = new ResponseModel(RespEnum.RESP_FAIL.isBooleanCode(),data,msg);
		responseModel.setCode(code);
		return responseModel;
	}
}
